<template>
    <div id="backg">
        <h1 class="title">欢迎</h1>
        <van-form @submit="onSubmit" class="login-form">
            <van-field
                v-model="user_name"
                name="user_name"
                label="姓名"
                placeholder="您的姓名"
                :rules="[{ required: true, message: '请填写姓名' }]"
            />
            <van-field
                v-model="user_phone"
                name="user_phone"
                label="手机号"
                placeholder="您的手机号"
                :rules="[{ required: true, message: '请填写手机号' }]"
            />
            <van-field
                v-model="user_number"
                name="user_number"
                label="工号"
                placeholder="您的工号"
                :rules="[{ required: true, message: '请填写工号' }]"
            />
            <van-field
                v-model="user_company"
                name="user_company"
                label="单位"
                placeholder="您的单位"
                :rules="[{ required: true, message: '请填写单位' }]"
            />
            <div style="margin: 16px;margin-top:20%">
                <van-button block type="primary" native-type="submit">提交</van-button>
            </div>
        </van-form>
        <div style="width:300px;margin:0 auto; padding:20px 0;">
          <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=13020302001048" style="display:inline-block;text-decoration:none;height:20px;line-height:20px;">
            <img src="../assets/countryicon.png" style="float:left;"/><p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">冀公网安备 13020302001048号</p >
          </a >
        </div>
        <a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener noreferrer" style="height:20px;line-height:20px;margin: 0px 0px 0px 5px; color:#939393;">冀ICP备2021009292号-2</a>
    </div>
</template>
<script>
// import "http://res.wx.qq.com/open/js/jweixin-1.6.0.js"
import axios from "axios";

export default {
data() {
    return {
      user_name: '',
      user_number: '',
      user_company: '',
      user_phone:'',
      openID:'',
      uid:'',
      fromURL:'',
      nowisEdit:false
    };
  },
  created(){
    let params = new URLSearchParams(location.search);
    var code = params.get('code')
    this.fromURL = params.get('fromURL')
    if(code){//从底部菜单来
      var _this = this;
      var data=axios(
        {
          url:"/api/home/user/openId",
          method:"post",
          data:{
            "uc":code
          },
          contentType:"text",
          processData: false,
          dataType:"text"
        }
      ).then(function (response){
        _this.openID = response.data.data.open_id
        window.localStorage.setItem("openID",_this.openID)
        _this.uid = response.data.data.uid;
        if(_this.uid!=0){//说明已经有信息了
            window.localStorage.setItem("uid",_this.uid)
            _this.nowisEdit=true;//说明现在是编辑页面
            _this.getUserInfo()
        }else{
            _this.nowisEdit=false;//说明现在是注册页面
        }
      })
    }
  },
  methods: {
    getUserInfo(){
      var _this = this;
      var data = axios({
        url:"/api/home/user/info",
        method:'post',
        data:{
          "user_id":_this.uid,
          "open_id":_this.openID
        }
      }).then(function(res){
        if(res.data.meta.ecode == 0){
          _this.user_name = res.data.data.user_name
          _this.user_phone = res.data.data.phone
          _this.user_company = res.data.data.company_name
          _this.user_number = res.data.data.employee_num
        }else{
          _this.$notify({ type: 'error', message: res.data.meta.emsg });
        }
      })
    },
    onSubmit(values) {
      var _this = this;
      var params = {
          method:"post",
          data:{
            "user_name":_this.user_name,
            "phone":_this.user_phone,
            "company_name":_this.user_company,
            "employee_num":_this.user_number
          },
          contentType:"text",
          processData: false,
          dataType:"text"
        }
        params.url = this.nowisEdit ? "/api/home/user/edit":"/api/home/user/reg"
        if(this.nowisEdit){
          params.data.user_id = this.uid
        }else{
          params.data.open_id = window.localStorage.getItem('openID')
        }
      var data=axios(params).then(function (response){
        if(response.data.meta.ecode == 0){
          _this.uid = response.data.data.uid;
          window.localStorage.setItem("uid",_this.uid)
          if(_this.fromURL){
            if(_this.fromURL == 'submitquestion'){
             _this.$router.push({name:_this.fromURL,query:{state:window.localStorage.getItem('param_point')}})//从哪来就到哪儿去
            }else{
              _this.$router.push({name:_this.fromURL})//从哪来就到哪儿去
            }
          }else{
            _this.$notify({ type: 'success', message: '提交成功' });
          }
        }else{
            _this.$notify({ type: 'error', message: response.data.meta.emsg });
        }
      })
    },
  },
}
</script>
<style>
#backg{
  background: url(./../assets/bg.png);
  background-size: 100% 100%;
 height: 100%;
 position: fixed;
 width: 100%;
 overflow: auto;
}

.login-form{
    width: 80%;
    margin: 0 auto;
    margin-top:4rem;
}
.login-form label,
.login-form input{
    font-size: 24px;
}
.van-cell{
    line-height: 2;
}
</style>
